﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace Project
{
    public partial class _Default : System.Web.UI.Page
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["StudentAdvisorString"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command_register = null;
        SqlCommand command_profile = null;

        protected void Page_Load(object sender, EventArgs e)
        {
           //session code (Active_mode)
        }

        protected void btn_clear_Click(object sender, EventArgs e)
        {
            txt_studentID.Text = "";
            txt_firstName.Text = "";
            txt_lastName.Text = "";
            txt_password.Text = "";
            txt_confirmPassword.Text = "";
            txt_email.Text = "";
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            string register_string = "insert into tblUserLogin with (XLOCK) (UserId,Password,Email,UserType,answer1,answer2,tempPassword) values (@studentID,@password,@email,@userType,@ans1,@ans2,default)";
            command_register = new SqlCommand(register_string, connection);
            string profile_string = "insert into tblStudentInfo with (XLOCK) (StudentId,StudentFirstName,StudentLastName,ProgramCode,EmailPrimary,International) values (@studentID,@firstName,@lastName,@programCode,@email,@international)";
            command_profile = new SqlCommand(profile_string, connection);

            command_register.Parameters.AddWithValue("@studentID", txt_studentID.Text);
            command_register.Parameters.AddWithValue("@password", txt_password.Text);
            command_register.Parameters.AddWithValue("@email", txt_email.Text);
            command_register.Parameters.AddWithValue("@userType", "S");
            command_register.Parameters.AddWithValue("@ans1", txt_answer1.Text);
            command_register.Parameters.AddWithValue("@ans2", txt_answer2.Text);
            //command_register.Parameters.AddWithValue("@temp", "default");

            command_profile.Parameters.AddWithValue("@studentID", txt_studentID.Text);
            command_profile.Parameters.AddWithValue("@firstName", txt_firstName.Text);
            command_profile.Parameters.AddWithValue("@lastName", txt_lastName.Text);
            command_profile.Parameters.AddWithValue("@programCode",txt_programCode.Text);
            command_profile.Parameters.AddWithValue("@email", txt_email.Text);
            command_profile.Parameters.AddWithValue("@international", 0);

            try
            {
                connection.Open();
                command_register.ExecuteNonQuery();
                command_profile.ExecuteNonQuery();
                connection.Close();
                lbl_thankyou.Text = "Thank you for your registration...";
            }
            catch (SqlException ex)
            {
                lbl_thankyou.Text = ex.ToString()+"This Student ID is already registered.";
            }
            finally
            {
                txt_studentID.Text = "";
                txt_firstName.Text = "";
                txt_lastName.Text = "";
                txt_programCode.Text = "";
                txt_password.Text = "";
                txt_confirmPassword.Text = "";
                txt_email.Text = "";
            }
        }
    }
}